package regAlloc;

import java.util.ArrayList;

import temp.Temp;
import temp.TempList;
import temp.TempMap;

public class RegAlloc implements TempMap {
	public assem.InstrList instrs;
	regAlloc.Color allocation;

	public String tempMap(Temp t) {
		return allocation.tempMap(t);
	}
	
	public assem.InstrList rebuildInstr(mips.Frame frame, assem.InstrList il) {
		return allocation.rebuildProg(frame, il);
	}

	public RegAlloc(frame.Frame f, assem.InstrList il) {
		flowGraph.AssemFlowGraph fg = new flowGraph.AssemFlowGraph(il);
//		fg.show(System.out);
		regAlloc.Liveness ln = new regAlloc.Liveness(fg);
		TempList specialReg = null;
		ArrayList<temp.Temp> arg = f.getArgRegs();
		for (int i = 0; i < arg.size(); i++)
			specialReg = new TempList(arg.get(i), specialReg);
		for (TempList i = f.getCalleeSaveRegs(); i != null; i = i.tail)
			specialReg = new TempList(i.head, specialReg);
		for (TempList i = f.getCallerSaveRegs(); i != null; i = i.tail)
			specialReg = new TempList(i.head, specialReg);
		allocation = new regAlloc.Color(ln, specialReg);
	}
	
}
